package P200;

import org.junit.Test;

/**
 * @Author DJ同学
 * @Date 2021/2/10 22:47
 * @Version 1.0
 * @Name 岛屿数量
 * @Problem https://leetcode-cn.com/problems/number-of-islands/
 * @idea DFS
 */
public class Solution2 {

    @Test
    public void test(){
        char[][] grid={{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}} ;
        System.out.println(numIslands(grid));
    }

    public int numIslands(char[][] grid) {
        int cnt = 0;
        for(int i =0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
                if(grid[i][j]=='1'){
                    cnt++;
                    dfs(grid,i,j);
                }
            }
        }
        return cnt;
    }
    void dfs(char[][] grid,int i,int j){
        if(i>=grid.length||j>=grid[0].length||i<0||j<0||grid[i][j]=='0'){
            return ;
        }
        grid[i][j]='0';
        dfs(grid,i,j+1);
        dfs(grid,i,j-1);
        dfs(grid,i+1,j);
        dfs(grid,i-1,j);
    }
}
